package me.flyray.bsin.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import me.flyray.bsin.domain.entity.BizRoleTypeChainCoin;
import me.flyray.bsin.domain.request.CustomerChainCoinDTO;
import me.flyray.bsin.domain.response.CustomerChainCoinVO;
import me.flyray.bsin.exception.BusinessException;
import me.flyray.bsin.facade.service.BizRoleTypeChainCoinService;
import me.flyray.bsin.infrastructure.mapper.BizRoleTypeChainCoinMapper;
import me.flyray.bsin.mybatis.utils.Pagination;
import me.flyray.bsin.security.contex.LoginInfoContextHelper;
import me.flyray.bsin.security.domain.LoginUser;
import org.apache.shenyu.client.apache.dubbo.annotation.ShenyuDubboService;
import org.apache.shenyu.client.apidocs.annotations.ApiDoc;
import org.apache.shenyu.client.apidocs.annotations.ApiModule;
import org.apache.shenyu.client.dubbo.common.annotation.ShenyuDubboClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
* @author Admin
* @description 针对表【merchant_chain_coin(商户链上货币;)】的数据库操作Service实现
* @createDate 2024-04-29 15:44:26
*/
@Slf4j
@ApiModule(value = "bizRoleTypeChainCoin")
@ShenyuDubboService("/bizRoleTypeChainCoin")
public class BizRoleTypeChainCoinServiceImpl implements BizRoleTypeChainCoinService {

    @Autowired
    private BizRoleTypeChainCoinMapper customerChainCoinMapper;

    @Override
    @ShenyuDubboClient("/add")
    @ApiDoc(desc = "add")
    public void add(CustomerChainCoinDTO customerChainCoinDTO) {
        log.info("请求CustomerChainCoinService.saveCustomerChainCoin,参数:{}", customerChainCoinDTO);
        LoginUser loginUser = LoginInfoContextHelper.getLoginUser();
        BizRoleTypeChainCoin customerChainCoin = new BizRoleTypeChainCoin();
        BeanUtils.copyProperties(customerChainCoinDTO,customerChainCoin);
        customerChainCoin.setTenantId(loginUser.getTenantId());
        customerChainCoin.setBizRoleTypeNo(loginUser.getBizRoleTypeNo());
        customerChainCoin.setBizRoleType(loginUser.getBizRoleType());
        customerChainCoin.setCreateTime(new Date());
        customerChainCoin.setCreateBy(loginUser.getBizRoleTypeNo());
        customerChainCoinMapper.insert(customerChainCoin);
        log.info("添加币种配置成功，租户ID: {}, 币种: {}", loginUser.getTenantId(), customerChainCoinDTO.getChainCoinNo());
    }

    @Override
    @ShenyuDubboClient("/edit")
    @ApiDoc(desc = "edit")
    public void edit(CustomerChainCoinDTO customerChainCoinDTO) {
        log.info("请求CustomerChainCoinService.updateCustomerChainCoin,参数:{}", customerChainCoinDTO);
        LoginUser user = LoginInfoContextHelper.getLoginUser();
        BizRoleTypeChainCoin customerChainCoin = new BizRoleTypeChainCoin();
        BeanUtils.copyProperties(customerChainCoinDTO,customerChainCoin);
        customerChainCoin.setUpdateTime(new Date());
        customerChainCoin.setUpdateBy(user.getUserId());
        customerChainCoinMapper.updateById(customerChainCoin);
        log.info("编辑币种配置成功，配置ID: {}", customerChainCoinDTO.getSerialNo());
    }

    @Override
    @ShenyuDubboClient("/delete")
    @ApiDoc(desc = "delete")
    public void delete(CustomerChainCoinDTO customerChainCoinDTO) {
        log.info("请求CustomerChainCoinService.deleteCustomerChainCoin,参数:{}", customerChainCoinDTO);
        BizRoleTypeChainCoin customerChainCoin = new BizRoleTypeChainCoin();
        BeanUtils.copyProperties(customerChainCoinDTO,customerChainCoin);
        LoginUser loginUser = LoginInfoContextHelper.getLoginUser();
        customerChainCoin.setUpdateTime(new Date());
        customerChainCoin.setUpdateBy(loginUser.getUserId());
        customerChainCoinMapper.updateDelFlag(customerChainCoin);
        log.info("删除币种配置成功，配置ID: {}", customerChainCoinDTO.getSerialNo());
    }

    @Override
    @ShenyuDubboClient("/getList")
    @ApiDoc(desc = "getList")
    public  List<BizRoleTypeChainCoin> getList(CustomerChainCoinDTO customerChainCoinDTO) {
        log.debug("请求CustomerChainCoinService.getCustomerChainCoinList,参数:{}", customerChainCoinDTO);
        LoginUser loginUser = LoginInfoContextHelper.getLoginUser();
        LambdaQueryWrapper<BizRoleTypeChainCoin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BizRoleTypeChainCoin::getTenantId, loginUser.getTenantId())
                .eq(BizRoleTypeChainCoin::getBizRoleTypeNo, loginUser.getBizRoleTypeNo())
                .eq(BizRoleTypeChainCoin::getBizRoleType, loginUser.getBizRoleType())
                .orderByDesc(BizRoleTypeChainCoin::getCreateTime);
        return customerChainCoinMapper.selectList(queryWrapper);
    }

    @Override
    @ShenyuDubboClient("/getPageList")
    @ApiDoc(desc = "getPageList")
    public Page<CustomerChainCoinVO> getPageList(CustomerChainCoinDTO customerChainCoinDTO) {
        log.debug("请求CustomerChainCoinService.pageListByCustomerId,参数:{}", customerChainCoinDTO);
        LoginUser loginUser = LoginInfoContextHelper.getLoginUser();
        Pagination pagination = customerChainCoinDTO.getPagination();
        customerChainCoinDTO.setTenantId(loginUser.getTenantId());
        customerChainCoinDTO.setBizRoleTypeNo(loginUser.getBizRoleTypeNo());
        customerChainCoinDTO.setBizRoleType(loginUser.getBizRoleType());
        return customerChainCoinMapper.pageList(new Page<>(pagination.getPageNum(), pagination.getPageSize()), customerChainCoinDTO);
    }

}




